/*
* This file is part of RoboDS (http://code.google.com/p/robods/)
* Copyright (C) 2007 Blakharaz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include "DsUserInputState.h"

#include <PA9.h>

#include "DsCard.h"
#include "DsGame.h"
#include "DsSprite.h"
#include "DsUserInterface.h"
#include "Logger.h"
#include "OkButton.h"
#include "Robot.h"
#include "SpriteManager.h"

namespace robo
{

    DsUserInputState::DsUserInputState(DsGame* game, DsUserInterface* ui)
    :	UserInputState(game, ui),
	    mDraggedSprite(NULL)
    {
    }

    void DsUserInputState::resume()
    {
	    ///@todo deal new cards

	    getDsUi()->getOkButton()->setPressed(false);
	    getDsUi()->getOkButton()->setEnabled(true);
    }

    void DsUserInputState::pause()
    {
	    getDsUi()->getOkButton()->setPressed(false);
	    getDsUi()->getOkButton()->setEnabled(false);
    }

    void DsUserInputState::handleEvents()
    {
        if (Stylus.Newpress)
        {
		    DsSprite* touched = SpriteManager::getSingleton()->getTouchedSprite();

		    if (touched 
			    && getDsUi()->getOkButton() == touched 
			    && getDsUi()->getOkButton()->isEnabled())
		    {
			    switchGameState(GameState::ROBOT_MOVEMENT);
		    }
		    else if (getDsUi()->isPowerButton(touched))
		    {
			    if (getDsUi()->getRobot()->isEnabled())
			    {
				    getDsUi()->togglePowerButton();
			    }
		    }
		    else
		    {
			    mDraggedSprite = touched;
			    mDraggedSpriteOffset.X = Stylus.X - mDraggedSprite->getPosition().X;
			    mDraggedSpriteOffset.Y = Stylus.Y - mDraggedSprite->getPosition().Y;
		    }
        }

        if (Stylus.Held)
        {
            if (mDraggedSprite && mDraggedSprite->isMovable())
            {
                mDraggedSprite->setPosition(
				    Stylus.X - mDraggedSpriteOffset.X, 
				    Stylus.Y - mDraggedSpriteOffset.Y);
            }
        }

        if (Stylus.Released)
        {
		    draggedSprite(mDraggedSprite, Stylus.X, Stylus.Y);
            mDraggedSprite = NULL;
        }

	    ///@todo show pressed button
	    //static u16 FRAMES_PRESSED_BUTTON = 10;
	    //static u16 FRAMES_RELEASED_BUTTON = 5;
	    //case PHASE_SHOW_PRESSED_BUTTON:
	    //	getDsUi()->getOkButton()->setPressed(true);
	    //	if (mFrameCounter == FRAMES_PRESSED_BUTTON)
	    //	{
	    //		switchPhase(PHASE_SHOW_RELEASED_BUTTON);
	    //	}
	    //	break;
	    //case PHASE_SHOW_RELEASED_BUTTON:
	    //	getDsUi()->getOkButton()->setPressed(false);
	    //	if (mFrameCounter == FRAMES_RELEASED_BUTTON)
	    //	{
	    //		switchPhase(PHASE_MOVE_ROBOTS);
	    //		mCurrentCardNum = 0;
	    //	}
	    //	break;
	    // 	getDsUi()->getOkButton()->setEnabled(false);

    }

    void DsUserInputState::draggedSprite(DsSprite* sprite, u16 x, u16 y)
    {
	    DsCard* card = dynamic_cast<DsCard*>(sprite);
	    if (card)
	    {
		    Vector2 targetPos = card->getPosition();
		    int slot = getDsUi()->getCardSlotAt(x, y);
		    if (slot != UserInterface::NO_SLOT && slot != UserInterface::SLOT_AVAIL)
		    {
			    getDsUi()->setActiveCard(slot, card);
			    getDsUi()->update();
		    }
		    else if (slot == UserInterface::SLOT_AVAIL)
		    {
			    getDsUi()->addAvailableCard(card);
			    getDsUi()->update();
		    }
		    else if (slot == UserInterface::NO_SLOT)
		    {
			    getDsUi()->update();
		    }
	    }
    }

    DsUserInterface* DsUserInputState::getDsUi()
    {
        return static_cast<DsUserInterface*>(mUI);
    }

}
